package com.fbinggun.fattribute.attribute.defense;

import com.fbinggun.fattribute.FAttributePlugin;
import com.fbinggun.fattribute.attribute.ADefense;
import com.fbinggun.fattribute.data.DataManage;
import com.fbinggun.fattribute.data.EntityData;
import com.fbinggun.fbinggunutil.RandomUtil;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.EntityDamageByEntityEvent;

public class Dodge extends ADefense {


    @Override
    public String getKey() {
        return FAttributePlugin.plugin.getConfigKey("Dodge");
    }

    @Override
    public int getCombat() {
        return 0;
    }
    @Override
    public String getID() {
        return "Dodge";
    }
    @Override
    public double getDefense(EntityData data, EntityDamageByEntityEvent event) {
        if (event.getDamager() instanceof LivingEntity) {
            LivingEntity livingEntity = (LivingEntity) event.getDamager();
            EntityData entityData = DataManage.getData(livingEntity);
            double dodge = getValue(data) * 0.01;
            dodge = dodge - entityData.getValue(FAttributePlugin.plugin.getConfigKey("Hit")) * 0.01;
            dodge = Math.max(0.0001, dodge);
            if (RandomUtil.is(dodge)) {
                event.setCancelled(true);
                return 0;
            }
        }
        return 0;
    }

}
